package com.market.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.market.domain.transients.Email;

@Entity
@Table(name = "message")
public class Message extends BaseObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7782756802803644669L;

	@Override
	public boolean equalsId(Object o) {
		if (o instanceof Message && ((Message) o).getId().equals(this.id)) {
			return true;
		}
		return false;
	}

	public static enum TYPE {
		BLOG, ADDVERTISEMENT, URGENT, INDIVIDUALS;
		private String getMessage(String key) {
			Locale locale = FacesContext.getCurrentInstance().getViewRoot()
					.getLocale();
			java.util.ResourceBundle resBundle = ResourceBundle.getBundle(
					"com.market.messages", locale);
			return resBundle.getString(key);
		}

		public String getLabel() {
			return this.getMessage("MESSAGE_TYPE_" + this.toString());
		}
	}

	@Transient
	public String getTitleLabel() {
		return super.getLabel("Title", super.getLocale());
	}

	@Transient
	public String getTitleLabel(Locale locale) {
		return super.getLabel("Title", locale);
	}

	@Transient
	public String getContentLabel() {
		return super.getLabel("Content", super.getLocale());
	}

	@Transient
	public String getContentLabel(Locale locale) {
		return super.getLabel("Content", locale);
	}

	@Transient
	public Picture getPrimaryPicture() {
		if (this.pictures != null && this.pictures.size() > 0) {
			return this.pictures.get(0);
		}
		return null;
	}

	/**
	 * This method add related pictures to email's content
	 * 
	 * @param pictures
	 * @param content
	 * @param userLocale
	 * @return
	 */
	private String addPicturesToContent(List<Picture> pictures, String content,
			Locale userLocale) {
		String pictureTitle;
		String pictureDesc;
		for (Picture picture : pictures) {
			content = content + "\r\n<br>";
			content = content + "<img src=\"http://QuebecFresh.com/"
					+ picture.getFullFileName() + "\">";
			pictureTitle = picture.getTitleLabel(userLocale);
			if (pictureTitle != null && !pictureTitle.isEmpty()) {
				content = content + "<font size=\"4\" color=\"green\">"
						+ pictureTitle + "</font>";
				content = content + "<br>";
			}
			pictureDesc = picture.getDescriptionLabel(userLocale);

			if (pictureDesc != null && !pictureDesc.isEmpty()) {
				content = content + "<font size=\"2\" color=\"blue\">"
						+ pictureDesc + "</font>";
				content = content + "\r\n<br>";
			}

		}
		return content;
	}

	/**
	 * 
	 * @return： A list of email that will be sent to all users related to this
	 *          message;
	 */
	@Transient
	public List<Email> getEmails() {
		List<Email> emails = new ArrayList<Email>();
		String subject;
		String content;
		for (User user : this.users) {
			if (user.getIsEnabled()) { //If this user was disabled, don't send email.
				Email email = new Email();
				subject = this.getTitleLabel(user.getLanguage().getLocale());
				content = this.getContentLabel(user.getLanguage().getLocale());
				content = this.addPicturesToContent(this.getPictures(),
						content, user.getLanguage().getLocale());
				email.setSubject(subject);
				email.setContent(content);
				email.setFrom(this.getMessage("ourEmail"));
				email.setWebSiteName(super.getMessage("webSiteName"));
				email.setTo(user.getEmail());
				emails.add(email);
			}
		}
		return emails;
	}

	public Email getEmails(User user) {
		Email email = new Email();
		String subject = this.getTitleLabel(user.getLanguage().getLocale());
		String content = this.getContentLabel(user.getLanguage().getLocale());
		content = this.addPicturesToContent(this.getPictures(), content, user
				.getLanguage().getLocale());
		email.setSubject(subject);
		email.setContent(content);
		email.setFrom(this.getMessage("ourEmail"));
		email.setWebSiteName(super.getMessage("webSiteName"));
		email.setTo(user.getEmail());
		return email;
	}

	public Boolean getIsHaveUser() {
		if (this.users != null && this.users.size() > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsHaveLocation() {
		if (this.locations != null && this.locations.size() > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	@Temporal(TemporalType.TIMESTAMP)
	private java.util.Date publishedDate;
	@Column(length = 300)
	private String title;
	@Column(length = 300)
	private String chineseTitle;
	@Column(length = 300)
	private String frenchTitle;
	@Enumerated(EnumType.STRING)
	@Column(length = 30)
	private TYPE type = TYPE.BLOG;
	@Column(length = 1000)
	private String content;
	@Column(length = 1000)
	private String chineseContent;
	@Column(length = 1000)
	private String frenchContent;
	@ManyToMany(fetch = FetchType.LAZY)
	List<User> users = new ArrayList<User>();
	@ManyToMany(fetch = FetchType.LAZY)
	List<Location> locations = new ArrayList<Location>();
	@ManyToMany(fetch = FetchType.LAZY)
	List<Picture> pictures = new ArrayList<Picture>();

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((chineseContent == null) ? 0 : chineseContent.hashCode());
		result = prime * result
				+ ((chineseTitle == null) ? 0 : chineseTitle.hashCode());
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result
				+ ((frenchContent == null) ? 0 : frenchContent.hashCode());
		result = prime * result
				+ ((frenchTitle == null) ? 0 : frenchTitle.hashCode());
		result = prime * result
				+ ((publishedDate == null) ? 0 : publishedDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (chineseContent == null) {
			if (other.chineseContent != null)
				return false;
		} else if (!chineseContent.equals(other.chineseContent))
			return false;
		if (chineseTitle == null) {
			if (other.chineseTitle != null)
				return false;
		} else if (!chineseTitle.equals(other.chineseTitle))
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (frenchContent == null) {
			if (other.frenchContent != null)
				return false;
		} else if (!frenchContent.equals(other.frenchContent))
			return false;
		if (frenchTitle == null) {
			if (other.frenchTitle != null)
				return false;
		} else if (!frenchTitle.equals(other.frenchTitle))
			return false;
		if (publishedDate == null) {
			if (other.publishedDate != null)
				return false;
		} else if (!publishedDate.equals(other.publishedDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Message [id=" + id + ", publishedDate=" + publishedDate
				+ ", title=" + title + ", chineseTitle=" + chineseTitle
				+ ", frenchTitle=" + frenchTitle + ", type=" + type
				+ ", content=" + content + ", chineseContent=" + chineseContent
				+ ", frenchContent=" + frenchContent + "]";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public java.util.Date getPublishedDate() {
		return publishedDate;
	}

	public void setPublishedDate(java.util.Date publishedDate) {
		this.publishedDate = publishedDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getChineseTitle() {
		return chineseTitle;
	}

	public void setChineseTitle(String chineseTitle) {
		this.chineseTitle = chineseTitle;
	}

	public String getFrenchTitle() {
		return frenchTitle;
	}

	public void setFrenchTitle(String frenchTitle) {
		this.frenchTitle = frenchTitle;
	}

	public TYPE getType() {
		return type;
	}

	public void setType(TYPE type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getChineseContent() {
		return chineseContent;
	}

	public void setChineseContent(String chineseContent) {
		this.chineseContent = chineseContent;
	}

	public String getFrenchContent() {
		return frenchContent;
	}

	public void setFrenchContent(String frenchContent) {
		this.frenchContent = frenchContent;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public List<Location> getLocations() {
		return locations;
	}

	public void setLocations(List<Location> locations) {
		this.locations = locations;
	}

	public List<Picture> getPictures() {
		return pictures;
	}

	public void setPictures(List<Picture> pictures) {
		this.pictures = pictures;
	}

}
